﻿using System;
using System.Text.RegularExpressions;

namespace TomNet.Extensions
{
    public static class VerifyHelper
    {
        /// <summary>
        /// 验证是否时bool类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsBoolean(string value)
        {
            return bool.TryParse(value, out bool result);
        }

        /// <summary>
        /// 验证日期是否合法
        /// </summary>
        /// <param name="date">日期</param>
        public static bool IsDate(string strDate)
        {
            try
            {
                DateTime.Parse(strDate);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证正整数
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsInteger(string txt)
        {
            Regex objReg = new Regex(@"^[1-9]\d*$");
            return objReg.IsMatch(txt);
        }

        /// <summary>
        /// 验证是否是Email
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsEmail(string txt)
        {
            Regex objReg = new Regex(@"/^[!#$%'*+\-\/0-9=?A-Z^_`a-z{}~]+(\.[!#$%'*+\-\/0-9=?A-Z^_`a-z{}~]+)*@(?![\d]+(\.[\d]+)*$)[!#$%'*\/0-9=?A-Z^`a-z{}~]+([!#$%'*\-\/0-9=?A-Z^_`a-z{}~]*[!#$%'*\/0-9=?A-Z^`a-z{}~]+)*(\.[!#$%'*\/0-9=?A-Z^`a-z{}~]+([!#$%'*\-\/0-9=?A-Z^_`a-z{}~]*[!#$%'*\/0-9=?A-Z^`a-z{}~]+)*)+$/");
            return objReg.IsMatch(txt);
        }
    }
}
